<!DOCTYPE html>
<html>
<head>
<link rel="help" href="http://www.w3.org/TR/2012/WD-dom-20121206/#interface-domimplementation">
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script>
description("Checks that the DOMImplementation api arguments are correctly validated")

var testDocument, testPrototype, testNamespace, testQualifiedName, testDocType;
function validateDocument(document, prototype, namespace, qualifiedName, docType)
{
    testDocument = document;
    testPrototype = prototype;
    testNamespace = namespace;
    testQualifiedName = qualifiedName;
    testDocType = docType;

    shouldBe('testDocument.__proto__', 'window["' + prototype + '"].prototype');
    if (!testQualifiedName || testQualifiedName == "") {
        shouldBeNull('testDocument.documentElement');
    } else {
        shouldBeEqualToString('testDocument.documentElement.tagName', '' + qualifiedName);
        if (namespace)
            shouldBeEqualToString('testDocument.documentElement.namespaceURI', '' + namespace);
        else
            shouldBeNull('testDocument.documentElement.namespaceURI');
    }

    if (docType)
        shouldBe('testDocument.doctype', 'testDocType');
    else
        shouldBeNull('testDocument.doctype');
}

debug('\nDocumentType createDocumentType(DOMString qualifiedName, DOMString publicId, DOMString systemId);');
shouldThrow('document.implementation.createDocumentType()', '"TypeError: Failed to execute \'createDocumentType\' on \'DOMImplementation\': 3 arguments required, but only 0 present."');
shouldThrow('document.implementation.createDocumentType("qualifiedName")', '"TypeError: Failed to execute \'createDocumentType\' on \'DOMImplementation\': 3 arguments required, but only 1 present."');
shouldThrow('document.implementation.createDocumentType("qualifiedName", "publicId")', '"TypeError: Failed to execute \'createDocumentType\' on \'DOMImplementation\': 3 arguments required, but only 2 present."');
var docType;
shouldNotThrow('docType = document.implementation.createDocumentType("qualifiedName", "publicId", "systemId")');
shouldBe('docType.__proto__', 'DocumentType.prototype');
shouldBeEqualToString('docType.name', 'qualifiedName');
shouldBeEqualToString('docType.publicId', 'publicId');
shouldBeEqualToString('docType.systemId', 'systemId');

debug('\nXMLDocument createDocument(DOMString? namespace, [TreatNullAs=EmptyString] DOMString qualifiedName, DocumentType? doctype);');
shouldThrow('document.implementation.createDocument()', '"TypeError: Failed to execute \'createDocument\' on \'DOMImplementation\': 2 arguments required, but only 0 present."');
shouldThrow('document.implementation.createDocument("namespace")', '"TypeError: Failed to execute \'createDocument\' on \'DOMImplementation\': 2 arguments required, but only 1 present."');
shouldNotThrow('document.implementation.createDocument("namespace", "qualifiedName")');
var doc;
shouldNotThrow('doc = document.implementation.createDocument("namespace", "qualifiedName", null)');
validateDocument(doc, 'XMLDocument', 'namespace', 'qualifiedName', null);
shouldNotThrow('doc = document.implementation.createDocument(null, "qualifiedName", null)');
validateDocument(doc, 'XMLDocument', null, 'qualifiedName', null);
shouldNotThrow('doc = document.implementation.createDocument("", null, null)')
validateDocument(doc, 'XMLDocument', "", null, null);
shouldNotThrow('doc = document.implementation.createDocument("", "", null)')
validateDocument(doc, 'XMLDocument', "", "", null);
shouldNotThrow('doc = document.implementation.createDocument("namespace", "qualifiedName")');
validateDocument(doc, 'XMLDocument', "namespace", "qualifiedName", null);
shouldNotThrow('doc = document.implementation.createDocument("namespace", "qualifiedName", docType)');
validateDocument(doc, 'XMLDocument', "namespace", "qualifiedName", docType);

debug('\nDocument createHTMLDocument(optional DOMString title);');
shouldNotThrow('doc = document.implementation.createHTMLDocument()');
validateDocument(doc, 'HTMLDocument', "http://www.w3.org/1999/xhtml", "HTML", doc.doctype);
shouldNotThrow('doc = document.implementation.createHTMLDocument("title")');
validateDocument(doc, 'HTMLDocument', "http://www.w3.org/1999/xhtml", "HTML", doc.doctype);
</script>
</body>
</html>
